package arithmetic.thread.conn009;


import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.extern.slf4j.Slf4j;
import utils.ValidUtils;


/**
 * Created by jiangfeng on 2017/10/16. ģ����������
 */
public class MyArrayBlockQuenen {

    public static Logger log = LoggerFactory.getLogger(ValidUtils.class);

    // ����
    private LinkedList list = new LinkedList();
    // ���г���
    private final int minLength = 0;
    private final int maxLength;

    public MyArrayBlockQuenen(int size) {
        this.maxLength = size;
    }

    // ������
    private AtomicInteger count = new AtomicInteger(0);
    // ������
    private Object lock = new Object();

    public void put(Object obj) {
        synchronized (lock) {
            while (count.get() == maxLength) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.add(obj);
            count.getAndIncrement();
            lock.notify();
            System.out.println(Thread.currentThread().getName() + ":put " + obj);
        }
    }

    public Object get() {
        Object obj = null;
        synchronized (lock) {
            while (count.get() == minLength) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            obj = list.removeFirst();
            count.decrementAndGet();
            //3 ֪ͨ����һ���̣߳����ѣ�
            lock.notify();
            System.out.println(Thread.currentThread().getName() + ":get " + obj);
        }
        return obj;
    }

    public int getSize() {
        return list.size();
    }


    public static void main(String[] args) {
        MyArrayBlockQuenen quenen = new MyArrayBlockQuenen(5);

        quenen.put('a');
        quenen.put('b');
        quenen.put('c');
        quenen.put('d');
        quenen.put('e');
        System.out.println("main: " + quenen.getSize());
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                quenen.put('f');
                quenen.put('g');
                quenen.put('h');
                for (int i = 0; i < 200; i++) {
                     quenen.put(i);
                    log.info("{},put:{}",Thread.currentThread().getName(),i);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    Object o = quenen.get();
                    log.info("{},get:{}",Thread.currentThread().getName(),o);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }, "t2");

        try {
            // Thread.sleep(500l);
            TimeUnit.SECONDS.sleep(2);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();


    }

}
